import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class Main {
    private static ArrayList<Student> students = new ArrayList<>();
    private static ArrayList<Teacher> teachers = new ArrayList<>();

    public static void main(String[] args) {
        // 预填充一些学生和教师数据
        students.add(new Student("1", "张三", "班级1", 20, 170.5, "password123"));
        students.add(new Student("2", "李四", "班级2", 21, 180.0, "password123"));

        teachers.add(new Teacher("t1", "王老师", 35, "数学", "password123", "上午 9 点-10 点"));
        teachers.add(new Teacher("t2", "赵老师", 40, "物理", "password123", "下午 2 点到 4 点"));

        JFrame frame = new JFrame("用户管理系统");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 300);

        JPanel panel = new JPanel();
        frame.add(panel);
        placeComponents(panel);

        frame.setVisible(true);
    }

    private static void placeComponents(JPanel panel) {
        panel.setLayout(null);

        JLabel userLabel = new JLabel("用户类型:");
        userLabel.setBounds(10, 20, 80, 25);
        panel.add(userLabel);

        String[] userTypes = { "管理员", "学生", "教师" };
        JComboBox<String> userComboBox = new JComboBox<>(userTypes);
        userComboBox.setBounds(100, 20, 165, 25);
        panel.add(userComboBox);

        JLabel usernameLabel = new JLabel("用户名:");
        usernameLabel.setBounds(10, 50, 80, 25);
        panel.add(usernameLabel);

        JTextField usernameText = new JTextField(20);
        usernameText.setBounds(100, 50, 165, 25);
        panel.add(usernameText);

        JLabel passwordLabel = new JLabel("密码:");
        passwordLabel.setBounds(10, 80, 80, 25);
        panel.add(passwordLabel);

        JPasswordField passwordText = new JPasswordField(20);
        passwordText.setBounds(100, 80, 165, 25);
        panel.add(passwordText);

        JButton loginButton = new JButton("登录");
        loginButton.setBounds(10, 110, 80, 25);
        panel.add(loginButton);

        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String selectedUser = (String) userComboBox.getSelectedItem();
                String username = usernameText.getText();
                String password = new String(passwordText.getPassword());

                if (selectedUser.equals("管理员") && username.equals("admin") && password.equals("admin123")) {
                    AdminPanel adminPanel = new AdminPanel(students, teachers);
                    adminPanel.setVisible(true);
                } else if (selectedUser.equals("学生")) {
                    Student student = findStudentByIdAndPassword(username, password);
                    if (student != null) {
                        StudentPanel studentPanel = new StudentPanel(student);
                        studentPanel.setVisible(true);
                    } else {
                        JOptionPane.showMessageDialog(null, "用户名或密码错误");
                    }
                } else if (selectedUser.equals("教师")) {
                    Teacher teacher = findTeacherByIdAndPassword(username, password);
                    if (teacher != null) {
                        TeacherPanel teacherPanel = new TeacherPanel(teacher);
                        teacherPanel.setVisible(true);
                    } else {
                        JOptionPane.showMessageDialog(null, "用户名或密码错误");
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "用户名或密码错误");
                }
            }
        });
    }

    private static Student findStudentByIdAndPassword(String id, String password) {
        for (Student student : students) {
            if (student.getId().equals(id) && student.getPassword().equals(password)) {
                return student;
            }
        }
        return null;
    }

    private static Teacher findTeacherByIdAndPassword(String id, String password) {
        for (Teacher teacher : teachers) {
            if (teacher.getId().equals(id) && teacher.getPassword().equals(password)) {
                return teacher;
            }
        }
        return null;
    }
}
